package com.loong.android.http.request;

import com.loong.android.http.RequestEvent;
import com.loong.android.http.response.StringResponse;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Progress;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.PostRequest;
import com.lzy.okgo.request.base.Request;

import java.io.File;
import java.util.Arrays;

import loong.lang.Array;
import loong.lang.JSON;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * post请求类
 */
public class StringPostRequest extends PostRequest<String> {

    public StringPostRequest(String url) {
        super(url);
    }

    private StringCallback listen(RequestEvent.PostEvent callback) {
        final StringPostRequest req = this;
        return new StringCallback() {
            @Override
            public void onStart(Request<String, ? extends Request> request) {
                super.onStart(request);
                if(callback != null) callback.onStart(req);
            }

            @Override
            public void onSuccess(Response<String> response) {
                if(callback != null) callback.onSuccess(new StringResponse(response));
            }

            @Override
            public void onCacheSuccess(Response<String> response) {
                super.onCacheSuccess(response);
                if(callback != null) callback.onCacheSuccess(new StringResponse(response));
            }

            @Override
            public void uploadProgress(Progress progress) {
                super.uploadProgress(progress);
                if(callback != null) callback.onUploadProgress(progress);
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                if(callback != null) callback.onError(new StringResponse(response));
            }

            @Override
            public void onFinish() {
                super.onFinish();
                if(callback != null) callback.onFinish();
            }
        };
    }

    /**
     * 添加请求体参数
     * @param contentType Content-Type，如：application/json、application/x-www-form-urlencoded、text/plain
     * @param postData 提交的数据
     */
    public StringPostRequest upRequestBody(String contentType, String postData) {
        super.upRequestBody(RequestBody.create(MediaType.parse(contentType), postData));
        return this;
    }

    /**
     * 添加请求体参数
     * @param contentType Content-Type，如：application/json、application/x-www-form-urlencoded、text/plain
     * @param postData 提交的数据
     */
    public StringPostRequest upRequestBody(String contentType, File postData) {
        super.upRequestBody(RequestBody.create(MediaType.parse(contentType), postData));
        return this;
    }

    /**
     * 添加请求体参数
     * @param contentType Content-Type，如：application/json、application/x-www-form-urlencoded、text/plain
     * @param postData 提交的数据
     */
    public StringPostRequest upRequestBody(String contentType, byte[] postData) {
        super.upRequestBody(RequestBody.create(MediaType.parse(contentType), postData));
        return this;
    }

    /**
     * 添加请求体参数
     * @param contentType Content-Type，如：application/json、application/x-www-form-urlencoded、text/plain
     * @param postData 提交的数据
     * @param offset 起始位置
     * @param byteCount 字节数
     */
    public StringPostRequest upRequestBody(String contentType, byte[] postData, int offset, int byteCount) {
        super.upRequestBody(RequestBody.create(MediaType.parse(contentType), postData, offset, byteCount));
        return this;
    }

    /**
     * 添加字节数组参数
     * @param postData 提交的数据
     * @param contentType Content-Type，如：application/json、application/x-www-form-urlencoded、text/plain
     */
    public StringPostRequest upBytes(byte[] postData, String contentType) {
        super.upBytes(postData, MediaType.parse(contentType));
        return this;
    }

    /**
     * 添加文本参数
     * @param postData 提交的数据
     * @param contentType Content-Type，如：application/json、application/x-www-form-urlencoded、text/plain
     */
    public StringPostRequest upString(String postData, String contentType) {
        super.upString(postData, MediaType.parse(contentType));
        return this;
    }

    /**
     * 添加文件参数
     * @param postData 提交的数据
     * @param contentType Content-Type，如：application/json、application/x-www-form-urlencoded、text/plain
     */
    public StringPostRequest upFile(File postData, String contentType) {
        super.upFile(postData, MediaType.parse(contentType));
        return this;
    }

    /**
     * 添加json对象参数
     * @param json json对象
     */
    public StringPostRequest upJson(JSON json) {
        super.upJson(json.toString());
        return this;
    }

    /**
     * 添加可变数组参数
     * @param array 可变数组
     */
    public StringPostRequest upJson(Array<?> array) {
        super.upJson(array.toString());
        return this;
    }

    /**
     * 设置事件回调
     * @param callback 事件回调
     */
    public StringPostRequest setCallback(RequestEvent.PostEvent callback) {
        super.setCallback(listen(callback));
        return this;
    }

    /**
     * 提交请求
     * @param callback 事件回调
     */
    public StringPostRequest execute(RequestEvent.PostEvent callback) {
        super.execute(listen(callback));
        return this;
    }

    /**
     * 添加一个key附带多个文件的参数
     * @param key 键名
     * @param files 文件数据数组
     */
    public StringPostRequest addFileParams(String key, File[] files) {
        super.addFileParams(key, Arrays.asList(files));
        return this;
    }
}
